--测试环境
--package.path = package.path .. ";?.lua"
--生产环境
package.path = "./plugin/AF93310R/utils.lua" .. ";..\\?.lua"
package.path = "./plugin/AF93310R/json.lua" .. ";..\\?.lua"
package.path = "./plugin/AF93310R/crc.lua" .. ";..\\?.lua"
require("utils")
require("json")
require("crc")

--接收的报文
rxBuf = {}
--读数据与解析数据对应
Step = 0

--读变量
--在每次手动读变量/刷新时调用，或被自定义命令调用
function GenerateGetRealVariables(sAddr,step)
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    Step = step
    if step == 0 then
        requestADU[1] = 0x5E
        requestADU[2] = 0x50
        requestADU[3] = 0x30
        requestADU[4] = 0x30
        requestADU[5] = 0x35
        requestADU[6] = 0x51
        requestADU[7] = 0x33
        -- crc = Cal_crc_half(requestADU,#requestADU)
        -- requestADU[8] = math.modf(crc%256)
        -- requestADU[9] = math.modf(crc/256)
        requestADU[8] = 0x47
        requestADU[9] = 0x53
        requestADU[10] = 0x0D
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
        
    elseif step == 1 then
        requestADU[1] = 0x5E
        requestADU[2] = 0x50
        requestADU[3] = 0x30
        requestADU[4] = 0x30
        requestADU[5] = 0x34
        requestADU[6] = 0x51
        requestADU[7] = 0x57
        requestADU[8] = 0x53
        requestADU[9] = 0x0D
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    --elseif step == 2 then 
        --......
    end
    return VariableMap
end

--自定义命令
function DeviceCustomCmd(sAddr, cmdName, cmdParam,step) 

    if (cmdName == "GetRealVariables") then --读寄存器
        return GenerateGetRealVariables(sAddr,step)
        --返回报文 
    elseif (cmdName == "SetVariables") then
        --[[
        local res = json.jsondecode(cmdParam)
        if res["XXX"] then
            rtu.WriteData(sAddr,funCode,startAddr,res["XXX"])
        end
        if res["YYY"] then
            rtu.WriteData(sAddr,funCode,startAddr,res["YYY"])
        end
        ]]
        -----
        -----
        -----
    end
end

--解析报文
function AnalysisRx(sAddr, rxBufCnt)
    local index = 1
    local mAddr = 0
    local mFunCode = 0

    local len = 0

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }
    if (rxBufCnt < 5) then
        rxBuf = {}
        return VariableMap  --字节数不足5位时返回 1 继续获取完整数据包
    end

    for index = 1, rxBufCnt, 1 do
        mAddr = rxBuf[index]
        mFunCode = rxBuf[index+1]
        if index <= rxBufCnt then
            if mAddr == 0x5E then
                if mFunCode == 0x44 then
                    len = (rxBuf[index+2]-0x30)*100+(rxBuf[index+3]-0x30)*10+(rxBuf[index+4]-0x30)
                    -- print("len",len)
                   local rxCrc = rxBuf[rxBufCnt-2]*256+rxBuf[rxBufCnt-1]
                --    print(string.format("0x%X", rxCrc))
                --    print(string.format("0x%X", Cal_crc_half(rxBuf,len+2)))
                    if rxCrc == Cal_crc_half(rxBuf,len+2) then
                        local Variable = {}
                        local varIndex = 5
                        local VariableIndex = 1
                        if Step == 0 then
                             ---------------------------UPS工作模式----------------------------------
                             local UPSWorkMode = (rxBuf[varIndex+VariableIndex]-0x30)*10+(rxBuf[varIndex+VariableIndex+1]-0x30)
                             varIndex = varIndex + 2
                             Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"UPSWorkMode",UPSWorkMode,UPSWorkMode)
                             VariableIndex = VariableIndex + 1
                              ---------------------------线电压1----------------------------------
                             local LineVoltageR =  (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                             varIndex = varIndex + 5
                             Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LineVoltageR",LineVoltageR,LineVoltageR)
                             VariableIndex = VariableIndex + 1
                              ---------------------------线电压2----------------------------------
                             local LineVoltageS =  (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                             varIndex = varIndex + 5
                             Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LineVoltageS",LineVoltageS,LineVoltageS)
                             VariableIndex = VariableIndex + 1
                              ---------------------------线电压3----------------------------------
                             local LineVoltageT =  (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                             varIndex = varIndex + 5
                             Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LineVoltageT",LineVoltageT,LineVoltageT)
                             VariableIndex = VariableIndex + 1
                              ---------------------------线频率----------------------------------
                             local LineFrequency =  (rxBuf[varIndex+VariableIndex]-0x30)*100+(rxBuf[varIndex+VariableIndex+1]-0x30)*10+rxBuf[varIndex+VariableIndex+3]-0x30
                             varIndex = varIndex + 4
                             Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LineFrequency",LineFrequency,LineFrequency)
                             VariableIndex = VariableIndex + 1
                              ---------------------------输出电压1----------------------------------
                            local OutputVoltageR =  (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputVoltageR",OutputVoltageR,OutputVoltageR)
                            VariableIndex = VariableIndex + 1
                            ---------------------------输出电压2----------------------------------
                            local OutputVoltageS =  (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputVoltageS",OutputVoltageS,OutputVoltageS)
                            VariableIndex = VariableIndex + 1
                            ---------------------------输出电压3----------------------------------
                            local OutputVoltageT =  (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputVoltageT",OutputVoltageT,OutputVoltageT)
                            VariableIndex = VariableIndex + 1
                            ---------------------------输出频率----------------------------------
                            local OutputFrequency =  (rxBuf[varIndex+VariableIndex]-0x30)*100+(rxBuf[varIndex+VariableIndex+1]-0x30)*10+rxBuf[varIndex+VariableIndex+3]-0x30
                            varIndex = varIndex + 4
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputFrequency",OutputFrequency,OutputFrequency)
                            VariableIndex = VariableIndex + 1
                            ---------------------------输出电流1----------------------------------
                            local OutputCurrentR = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputCurrentR",OutputCurrentR,OutputCurrentR)
                            VariableIndex = VariableIndex + 1
                            ---------------------------输出电流2----------------------------------
                            local OutputCurrentS = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputCurrentS",OutputCurrentS,OutputCurrentS)
                            VariableIndex = VariableIndex + 1
                            ---------------------------输出电流3----------------------------------
                            local OutputCurrentT = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutputCurrentT",OutputCurrentT,OutputCurrentT)
                            VariableIndex = VariableIndex + 1
                            ---------------------------负载百分比1----------------------------------
                            local LoadPercentR = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LoadPercentR",LoadPercentR,LoadPercentR)
                            VariableIndex = VariableIndex + 1
                            ---------------------------负载百分比2---------------------------------
                            local LoadPercentS = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LoadPercentS",LoadPercentS,LoadPercentS)
                            VariableIndex = VariableIndex + 1
                            ---------------------------负载百分比3---------------------------------
                            local LoadPercentT = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LoadPercentT",LoadPercentT,LoadPercentT)
                            VariableIndex = VariableIndex + 1
                            ---------------------------总负载百分比---------------------------------
                            local TotalLoadPercent = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TotalLoadPercent",TotalLoadPercent,TotalLoadPercent)
                            VariableIndex = VariableIndex + 1
                            ---------------------------电池电压P---------------------------------
                            local BatteryVoltageP = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVoltageP",BatteryVoltageP,BatteryVoltageP)
                            VariableIndex = VariableIndex + 1
                            ---------------------------电池电压N---------------------------------
                            local BatteryVoltageN = (rxBuf[varIndex+VariableIndex]-0x30)*1000+(rxBuf[varIndex+VariableIndex+1]-0x30)*100+(rxBuf[varIndex+VariableIndex+2]-0x30)*10+rxBuf[varIndex+VariableIndex+4]-0x30
                            varIndex = varIndex + 5
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVoltageN",BatteryVoltageN,BatteryVoltageN)
                            VariableIndex = VariableIndex + 1
                            ---------------------------最大温度---------------------------------
                            local MaxTemperature = (rxBuf[varIndex+VariableIndex]-0x30)*100+(rxBuf[varIndex+VariableIndex+1]-0x30)*10+rxBuf[varIndex+VariableIndex+2]-0x30
                            varIndex = varIndex + 3
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"MaxTemperature",MaxTemperature,MaxTemperature)
                            VariableIndex = VariableIndex + 1
                            ---------------------------UPS类型---------------------------------
                            local UpsType = (rxBuf[varIndex+VariableIndex]-0x30)*10+rxBuf[varIndex+VariableIndex+1]-0x30
                            local type = ""
                            varIndex = varIndex + 1
                            print("UpsType",UpsType)
                            if UpsType == 0 then
                                type = "standby"
                            elseif UpsType == 1 then
                                type = "line-interactive"
                            elseif UpsType == 10 then
                                type = "on-line"
                            else
                            end
                            
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"UpsType",UpsType,type)
                            VariableIndex = VariableIndex + 1
                            ---------------------------设备故障---------------------------------
                            local UtilityFail = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"UtilityFail",UtilityFail,UtilityFail)
                            VariableIndex = VariableIndex + 1
                            ---------------------------电池电量低---------------------------------
                            local BatteryLow = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryLow",BatteryLow,BatteryLow)
                            VariableIndex = VariableIndex + 1
                            ---------------------------UPS的旁路模式---------------------------------
                            local BypassMode = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BypassMode",BypassMode,BypassMode)
                            VariableIndex = VariableIndex + 1
                            ---------------------------UPS发生故障或失效---------------------------------
                            local UPSFailed = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"UPSFailed",UPSFailed,UPSFailed)
                            VariableIndex = VariableIndex + 1
                            ---------------------------紧急关机按钮处于活动状态---------------------------------
                            local EPOActive = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EPOActive",EPOActive,EPOActive)
                            VariableIndex = VariableIndex + 1
                            ---------------------------测试正在进行中---------------------------------
                            local TestInProgress = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TestInProgress",TestInProgress,TestInProgress)
                            VariableIndex = VariableIndex + 1
                            ---------------------------正在进行关机操作---------------------------------
                            local ShutdownActive = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"ShutdownActive",ShutdownActive,ShutdownActive)
                            VariableIndex = VariableIndex + 1
                            ---------------------------静音状态---------------------------------
                            local MuteStatus = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"MuteStatus",MuteStatus,MuteStatus)
                            VariableIndex = VariableIndex + 1
                            ---------------------------电池测试正常---------------------------------
                            local BatteryTestOk = rxBuf[varIndex+VariableIndex]-0x30
                            -- varIndex = varIndex + 1
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryTestOk",BatteryTestOk,BatteryTestOk)
                            VariableIndex = VariableIndex + 1
                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                            rxBuf = {}
                            return VariableMap
                        end
                        if Step == 1 then
                            local  params1 = {"a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a16","a17","a18","a19","a20","a21",
                        "a22","a23","a24","a25","a26","a27","a28","a29","a30","a31","a32","a33","a34","a35","a36","a37","a38","a39","a40","a41","a42","a43","a44","a45","a46","a47",
                    "a48","a49","a50","a51","a52","a53","a54","a55","a56","a57","a58","a59","a60","a61","a62","a63","a64","a65","a66","a67","a68"}
                            for pIndex=1,#params1,1 do
                                local Value1 = rxBuf[varIndex+VariableIndex]-0x30
                                if Value1 >= 0 then
                                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,params1[pIndex],Value1,Value1)
                                else
                                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,params1[pIndex],0,0)
                                end
                                
                                VariableIndex = VariableIndex + 1
                            end
                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                            rxBuf = {}
                            return VariableMap
                        end
                    end
                else
                    index = index + 1
                end
            else
                index = index + 1
            end
            
        end
    end
    rxBuf = {}
    return VariableMap
end

-- -- 测试环境下调试发送指令
-- if (TestEnv() == true) then --
--     --GenerateGetRealVariables(01, 1)
--     --DeviceCustomCmd(01, "SetVariables", 0, 0)
--     AnalysisRx("01", 11)
-- end